// pages/totalRanking/totalRanking.js
import Toast from '@vant/weapp/toast/toast'
import {request} from '../../utils/request.js'
Page({
  subChange(event) {//科目变化
    this.checkSub = event.detail
    this.setData({
      checkSub: event.detail
    })
    this.findTotalScoreRankList()
  },
  classChange(event){//班级变化
    this.checkClass = event.detail
    this.setData({
      checkClass: event.detail
    })
    this.findTotalScoreRankList()
  },
  //考试信息
  findExamById(){
    let vm = this
    request({
      url:'/review-paper/exam/findExamById',
      data:{
        id: vm.examId
      },
      success(res){
        if(res.data.status == 1){
          //选中所有科目
          vm.checkSub = []
          res.data.data.subjectExamList.forEach(val => {
            vm.checkSub.push(val.id)
          })
          vm.subArray = res.data.data.subjectExamList
          vm.setData({
            checkSub: vm.checkSub,
            subArray: vm.subArray,
          })
          vm.findClassList()
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  //班级
  findClassList(){
    let vm = this
    request({
      url:'/review-paper/exam/findClassList',
      data:{
        schoolId: vm.data.schoolInfo.id,
        examId: vm.examId
      },
      success(res){
        if(res.data.status == 1){
          //选中所有班级
          vm.checkClass = []
          res.data.data.forEach(val => {
            vm.checkClass.push(val.id)
          })
          vm.classArray = res.data.data
          vm.setData({
            checkClass:vm.checkClass,
            classArray: vm.classArray
          })
          //排行榜表格头部
          vm.optionInit()
          vm.findTotalScoreRankList()
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  //排行榜
  findTotalScoreRankList(){
    let vm = this
    request({
      url:'/review-paper/statistics/findTotalScoreRankList',
      data:{
        examId:vm.examId,
        subjectExamIds: vm.checkSub.join(','),
        classIds:vm.checkClass.join(',')
      },
      success (res) {
        if(res.data.status == 1){
          res.data.data.forEach((val) => {
              //给分数拿到外层
              val.subjectScoreList.forEach((v) =>{
                  val[v.courseName] = v.totalScore
              })
              vm.subArray.forEach(v => {
                if(val[v.subjectName] === undefined){
                  val[v.subjectName] = '-'
                }
              })
              val.actFn = vm.navigateToView
          })
          vm.list = res.data.data
          vm.setData({
            list:vm.list
          })
        }else{
          Toast.fail(res.data.msg);
        }
      }
    })
  },
  cellTap(e){
    let choseData = null//判断是否点击的时科目
    this.subArray.forEach(val => {
      if(val.subjectName == e.detail.key){
        choseData = val
        choseData.studentId = e.detail.data.studentId
      }
    })
    if(choseData){
      this.navigateToView(choseData)
    }
  },
    //跳转试卷
    navigateToView(data){
      //传递科目信息
      wx.setStorage({
        key:'subjectInfo',
        data:data
      })
      wx.navigateTo({
        url: '../studentScoreView/studentScoreView'
      })
    },
  optionInit(){//排行榜配置初始化
    let vm = this
    let arr = [
    {title: '总排名', width:"60", key: 'schoolRank'}, 
    {title: '姓名', width:"60", key: 'realName'}, 
    {title: '总分', width:"60", key: 'totalScore'}, 
    {title: '班级', width:"60", key: 'className'},
    {title: '班级排名', width:"60", key: 'classRank'},
    {title: '班级学号', width:"100", key: 'studentNumber'},
    {title: '考号', width:"60", key: 'examNumber'}]
    this.subArray.forEach(val => {
      arr.push({title:val.subjectName, width:"60", key: val.subjectName,actFn:true})
    })
    this.setData({
      columns: arr
    })
  },
  data: {
    examId:'',
    checkClass:[],//班级
    checkSub:[],//科目
    subArray:[],//科目列表
    classArray:[],//班级列表
    option: {},
    list:[],
    schoolInfo:{},
    
    columns:[],//表头
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    this.examId = option.id
    let vm = this
    wx.getStorage({
      key:'schoolInfo',
      success(res){
        vm.setData({
          schoolInfo: res.data || {},
        })
        
        vm.findExamById()
      }
    })
  },
})